//
//  QLMShareHelper.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/31.
//  Copyright © 2017年 hwd. All rights reserved.
//


#import "QLMShareHelper.h"
#import <Social/Social.h>

@implementation QLMShareHelper

static QLMShareHelper * shareHelper;

#pragma mark - 单例
+ (instancetype)shareHelper{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareHelper = [[QLMShareHelper alloc]init];
    });
    return shareHelper;
}

#pragma mark - Public
+ (BOOL)shareWithType:(QLMShareHelperShareType)type andController:(UIViewController *)controller andFilePath:(NSString *)path{
    return [QLMShareHelper shareWithType:type andController:controller andFileURL:[NSURL fileURLWithPath:path]];
}

+ (BOOL)shareWithType:(QLMShareHelperShareType)type andController:(UIViewController *)controller andFileURL:(NSURL *)url{
    return [QLMShareHelper shareWithType:type andController:controller andItems:@[url]];
}

+ (BOOL)shareWithType:(QLMShareHelperShareType)type andController:(UIViewController *)controller andItems:(NSArray *)items{
    return [[QLMShareHelper shareHelper] shareWithType:type andController:controller andItems:items];
}

- (BOOL)shareWithType:(QLMShareHelperShareType)type andController:(UIViewController *)controller andItems:(NSArray *)items{
    
    //判断分享类型
    if(type==0){
        UIActivityViewController * activityCtl = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
        [controller presentViewController:activityCtl animated:YES completion:nil];
        return YES;
    }
    
    NSString * serviceType = [self serviceTypeWithType:type];
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:serviceType];
    // 添加要分享的图片
    
    for ( id obj in items){
        if ([obj isKindOfClass:[UIImage class]]){
            [composeVC addImage:(UIImage *)obj];
        }else if ([obj isKindOfClass:[NSURL class]]){
            [composeVC addURL:(NSURL *)obj];
        }
    }
    
    // 添加要分享的文字
    [composeVC setInitialText:@"分享"];
    
    // 弹出分享控制器
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"点击了发送");
        }
        else if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"点击了取消");
        }
    };
    
    @try{
        [controller presentViewController:composeVC animated:YES completion:nil];
        return YES;
    } @catch (NSException *exception){
        NSLog(@"没有安装");
        return NO;
    } @finally {
        
    }
    
    return YES;
}

#pragma mark - Private
- (NSString *)serviceTypeWithType:(QLMShareHelperShareType)type{
    //这个方法不再进行校验,传入就不等于0.这里做一个转换
    NSString * serviceType;
    if ( type!= 0){
        switch (type){
            case QLMShareHelperShareTypeWeChat:
                serviceType = @"com.tencent.xin.sharetimeline";
                break;
            case QLMShareHelperShareTypeQQ:
                serviceType = @"com.tencent.mqq.ShareExtension";
                break;
            case QLMShareHelperShareTypeSina:
                serviceType = @"com.apple.share.SinaWeibo.post";
                break;
            default:
                break;
        }
    }
    return serviceType;
}
@end
